$(document).ready(function(){
    var new_flg ="N";
    var update_flg ="N";
    var VAL ={
        NORMAL    	 : "0000000"  ,
        EVT_USER  	 : $("#userId").text(),
        T_FBPBISCUS  : 'FBPBISCUS',
        T_FBPBISMTR	 : 'FBPBISMTR',
        T_FPPWHSOPE	 : 'FPPWHSOPE',
        T_FBPBISDAT  : 'FBPBISDAT',
        DISABLED_ATTR : {
            "disabled" : true
        },
        ENABLED_ATTR : {
            "disabled" : false
        },
        DISABLED_FADETO :{
            "fast"   : 0.25
        },
        ENABLED_FADETO : {
            "fast" : 1.0
        }
    };

    var domObj = {
        ppBoxInfoDiv:$("#ppBoxInfoDiv"),
        ppBoxInfoGrd:$("#ppBoxInfoGrd"),
        ppBoxInfoPg	:"#ppBoxInfoPg"

    };

    var controlsQuery ={
        cusId 			: $("#cusId"),
        mtrlProdId 		: $("#mtrlProdId"),
        ppBoxStat		: $("#ppBoxStat"),
        stbShop			: $("#stbShop"),
        mtrlBoxId		: $("#mtrlBoxId"),
        mtrlThickness	: $("#mtrlThickness"),
        prdTlQty		: $("#prdTlQty"),
        mtrlLotId		: $("#mtrlLotId"),
        storageLoc      : $("#storageLoc"),

        queryBoxDialog_cusIdTxt :$("#queryBoxDialog_cusIdTxt"),
        queryBoxDialog_mtrlProdIdTxt:$("#queryBoxDialog_mtrlProdIdTxt")


    };
    var btnQuery = {
        query_btn 		: $("#query_btn"),
        delete_btn 		: $("#delete_btn"),
        add_btn 		: $("#add_btn"),
        update_btn		: $("#update_btn"),
        pick_btn   		: $("#pick_btn"),
        rwHouse_btn		: $("#returnWarehouse_btn"),
        rtGoods_btn		: $("#returnGoods_btn"),
        scrp_btn		: $("#scrp_btn"),
        commit_btn      : $("#commit_btn"),
        jqGridDel_btn	: $("#jqGridDel_btn"),
        clear_btn       : $("#clear_btn")
    };

    var iniGridInfo = function(){
        var ppBoxInfoGrdCM=[
            {name: 'mtrl_box_id',   	index: 'mtrl_box_id',   	label: DENSE_ID_TAG    , 	width: 100},
            {name: 'cus_id_fk',   		index: 'cus_id_fk',   		label: CUS_ID_TAG    ,   	width: 100},
            {name: 'mtrl_prod_id_fk',  index: 'mtrl_prod_id_fk',   label: MTRL_PROD_ID_TAG, 	width: 120},
            {name: 'mtrl_thickness',   index: 'mtrl_thickness',   	label: MTRL_THICKNESS_TAG,  width: 100},
            {name: 'prd_tl_qty',   	index: 'prd_tl_qty',   		label: GLASS_NUM_TAG    , 	width: 100},
            {name: 'prd_act_qty',   	index: 'prd_act_qty',   	label: "剩余数量"    , 	width: 100},
            {name: 'ppbox_stat',   	index: 'ppbox_stat',   		label: STATUS_TAG    , 	width: 100},
            {name: 'crt_timestamp',   	index: 'crt_timestamp',   	label: DENSE_CRT_TIMESTAMP    ,   width: 180},
            {name: 'mtrl_lot_id',   	index: 'mtrl_lot_id',   	label: BATCH_NO_TAG    , 	width: 100},
            {name: 'stb_shop',   		index: 'stb_shop',   		label: DEST_SHOP_TAG    , 	width: 100},
            {name: 'storage_loc',      index: 'storage_loc',       label: STORAGE_LOC_TAG,   width: 100}
        ];
        domObj.ppBoxInfoGrd.jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            height:200,
            width:domObj.ppBoxInfoDiv.width(),
            shrinkToFit:false,
            scroll:true,
            rownumWidth : true,
            resizable : true,
            rowNum:40,
            loadonce:true,
            fixed:true,
            viewrecords:true,
            multiselect : true,
            multiboxonly: true,
            pager : domObj.ppBoxInfoPg,
            colModel: ppBoxInfoGrdCM,
//            beforeSelectRow: function (rowid, e) {
//            	   var $myGrid = $(this),
//            	       i = $.jgrid.getCellIndex($(e.target).closest('td')[0]),
//            	       cm = $myGrid.jqGrid('getGridParam', 'colModel');
//            	   return (cm[i].name === 'cb');
//
//            	},
            onSelectRow: function(id){
                if(new_flg !="Y"){
                    toolFunc.boxGridSelRowFnc(id);
                }
            }
        });
    };
    var toolFunc = {

        initFnc : function(){
            $('input[type="text"]').val("");
            $("select").empty();
            $('input[type="text"]').attr(VAL.ENABLED_ATTR);
            $("select").attr(VAL.ENABLED_ATTR);
            domObj.ppBoxInfoGrd.jqGrid("clearGridData");
        },
        iniCusIdSel:function(cusIdObj){
            var inObj, outObj;
            var iary = {cus_typ : 'R'};

            inObj = {
                trx_id      : VAL.T_FBPBISCUS,
                action_flg  : 'Q',
                iary	: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(cusIdObj, outObj.oary, "cus_id","cus_name","",true);

            }
        },

        iniMtrlProdIdSel:function(){
            var inObj, outObj;
            var	cus_id = controlsQuery.cusId.val();
            inObj = {
                trx_id      : VAL.T_FBPBISMTR,
                action_flg  : 'Q',
                iary        : [{
                    mtrl_mkr	: cus_id,
                    mtrl_cate : "BARE"
                }]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(controlsQuery.mtrlProdId, outObj.oary, "mtrl_prod_id","mtrl_prod_id","",true);
            }
        },

        iniStbShopSel :function(){
	    	comAddValueByDataCateFnc("#stbShop","DEST","data_ext","data_desc",true);
        },

        iniBoxStatSel :function(){
            comAddValueByDataCateFnc("#ppBoxStat","DNST","data_id","data_desc",true);
        },
        iniStorageLocSel: function(){
            var stbShop = controlsQuery.stbShop.val();
            var inObj = {
                trx_id : VAL.T_FBPBISDAT,
                action_flg: 'Q',
                iary: [{
                    data_cate:'STLC',
                    data_id  :stbShop
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                SelectDom.addSelectArr(controlsQuery.storageLoc, outObj.oary, "data_ext","data_desc","",true);
                // toolFunc.setSelData(outObj.tbl_cnt, outObj.oary, "data_ext","data_desc",controlsQuery.storageLoc,true)
            }
        },
        boxGridSelRowFnc : function(rowId) {
            var rowData, iary, inObj, outObj, oary, i, tblCnt, oaryArr;
//			controlsQuery.mtrlBoxId.attr(VAL.DISABLED_ATTR);
            $('input[type="text"]').attr(VAL.DISABLED_ATTR);
            $("select").attr(VAL.DISABLED_ATTR);
            rowData = domObj.ppBoxInfoGrd.jqGrid("getRowData", rowId);
            mtrl_box_id = rowData.mtrl_box_id;

            inObj = {
                trx_id : VAL.T_FPPWHSOPE,
                action_flg : "Q",
                iary : [{
                    mtrl_box_id : mtrl_box_id
                }]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                oary = outObj.oary[0];
                if (!oary) {
                    return false;
                }
                controlsQuery.mtrlBoxId.val(oary.mtrl_box_id.trim());

                SelectDom.setSelect(controlsQuery.cusId, oary.cus_id_fk.trim());
                SelectDom.setSelect(controlsQuery.mtrlProdId, oary.mtrl_prod_id_fk.trim());
                SelectDom.setSelect(controlsQuery.ppBoxStat, oary.ppbox_stat.trim());
                SelectDom.setSelect(controlsQuery.stbShop, oary.stb_shop.trim());
                SelectDom.setSelect(controlsQuery.storageLoc, oary.storage_loc);

                controlsQuery.mtrlLotId.val(oary.mtrl_lot_id.trim());
                controlsQuery.prdTlQty.val(oary.prd_tl_qty.trim());
                controlsQuery.mtrlThickness.val(oary.mtrl_thickness.trim());

            }
        },
        updateGrdInput:function(rowIds,curStat){
            for (var int = 0; int < rowIds.length; int++) {
                domObj.ppBoxInfoGrd.jqGrid('setRowData',rowIds[int],{ppbox_stat:curStat});
            }
            SelectDom.setSelect(controlsQuery.ppBoxStat,curStat)
        }
    };

    var btnFunc = {
        query_func:function(){
            var iary = {};
            if(controlsQuery.mtrlBoxId.val()!=""){
                iary.mtrl_box_id  = controlsQuery.mtrlBoxId.val();
            }
            if(controlsQuery.cusId.find("option:selected").val()!=""){
                iary.cus_id_fk = controlsQuery.cusId.find("option:selected").val();
            }
            if(controlsQuery.mtrlProdId.find("option:selected").val()!=""){
                iary.mtrl_prod_id_fk = controlsQuery.mtrlProdId.find("option:selected").val();
            }
            if(controlsQuery.mtrlThickness.val()!=""){
                iary.mtrl_thickness  = controlsQuery.mtrlThickness.val();
            }
//    	    	if(controlsQuery.prdTlQty.val()!=""){
//    	    		iary.prd_tl_qty  = controlsQuery.prdTlQty.val();
//    	    	}
            if(controlsQuery.ppBoxStat.find("option:selected").val()!=""){
                iary.ppbox_stat  = controlsQuery.ppBoxStat.find("option:selected").val();
            }
            if(controlsQuery.mtrlLotId.val()!=""){
                iary.mtrl_lot_id  = controlsQuery.mtrlLotId.val();
            }
            if(controlsQuery.stbShop.find("option:selected").val()!=""){
                iary.stb_shop  = controlsQuery.stbShop.find("option:selected").val();
            }
            if(controlsQuery.storageLoc.find("option:selected").val()!=""){
                iary.storage_loc  = controlsQuery.storageLoc.find("option:selected").val();
            }

            var inTrxObj ={
                trx_id     : VAL.T_FPPWHSOPE ,
                action_flg : "Q"        ,
                iary       : [iary]
            }

            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                setGridInfo(outTrxObj.oary,"#ppBoxInfoGrd");
                var count =0;
                for(var i=0;i<outTrxObj.oary.length;i++){
                    count +=  parseInt(outTrxObj.oary[i].prd_act_qty,10);
                }
                $("#count").text(count);
            }

        },
        delete_func  : function(){
            var iary = new Array();
            var rowIds = domObj.ppBoxInfoGrd.jqGrid('getGridParam', 'selarrrow');
            if(rowIds.length === 0 ){
                showErrorDialog("","请选择需要删除的箱子！");
                return false;
            }
            btnQuery.delete_btn.showCallBackWarnningDialog({
                errMsg : "是否删除Densebox信息,请确认!!!!",
                callbackFn : function(data) {
                    var iaryA;
                    if (data.result == true) {
                        for (var int = 0; int < rowIds.length; int++) {
                            var rows = domObj.ppBoxInfoGrd.jqGrid('getRowData',rowIds[int]);
                            iaryA = {
                                mtrl_box_id:rows["mtrl_box_id"],
                                ppbox_stat :rows["ppbox_stat"]
                            };
                            iary[int] = iaryA;
                        }
                        inObj = {
                            trx_id : VAL.T_FPPWHSOPE,
                            action_flg : "D",
                            box_cnt : rowIds.length,
                            iary : iary
                        };
                        outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            showSuccessDialog("删除箱子信息成功");
                            for(var i = 0;i < rowIds.length ;i ++) {
                                domObj.ppBoxInfoGrd.jqGrid("delRowData", rowIds[i]);
                                i--;
                            }
                        }
                    }

                }
            });
        },
        update_func :function(){
            var rows = $("#ppBoxInfoGrd").jqGrid('getGridParam','selarrrow');
            if(rows.length <= 0){
                showErrorDialog("","请选择需要修改的DenseBox！");
                return;
            }else if(rows.length>1){
                showErrorDialog("","每次只能修改一个DenseBox信息！");
                return;
            }

            if (rows.length === 0) {
                showErrorDialog("","请在表中添加Densebox信息！");
                return;
            }
            var mtrlProdId = controlsQuery.mtrlProdId.val().trim();
            ppBoxStat = controlsQuery.ppBoxStat.val().trim();
            if(ppBoxStat != "IN"){
                showErrorDialog("", "不是[在库]状态，不可以修改！");
                return false;
            }
            update_flg = "Y";
            $('input[type="text"]').attr(VAL.ENABLED_ATTR);
            $("select").attr(VAL.ENABLED_ATTR);
            controlsQuery.mtrlBoxId.attr(VAL.DISABLED_ATTR);
            controlsQuery.ppBoxStat.attr(VAL.DISABLED_ATTR);
            controlsQuery.mtrlLotId.attr(VAL.DISABLED_ATTR);
            btnFunc.button_disabled(VAL.DISABLED_ATTR,0.25);
            btnQuery.commit_btn.attr(VAL.ENABLED_ATTR);
            btnQuery.commit_btn.fadeTo("fast",1.0);
        },
        commit_func :function(){
            if(new_flg == "Y"){
                var inObj,outObj,iary = [];
                var rows = $("#ppBoxInfoGrd").jqGrid("getRowData");
                if (rows.length === 0) {
                    showErrorDialog("","请在表中添加Densebox信息！");
                    return;
                }
                for (var i = 0; i < rows.length; i++) {
                    iary[i] = {
                        cus_id_fk 	    :   rows[i].cus_id_fk 	  ,
                        mtrl_prod_id_fk :   rows[i].mtrl_prod_id_fk,
                        ppbox_stat		:   rows[i].ppbox_stat		,
                        stb_shop		:   rows[i].stb_shop		  ,
                        storage_loc		:   rows[i].storage_loc		,
                        mtrl_box_id		:   rows[i].mtrl_box_id		,
                        mtrl_thickness	:   rows[i].mtrl_thickness	,
                        prd_tl_qty		:   rows[i].prd_tl_qty		,
                        mtrl_lot_id		:   rows[i].mtrl_lot_id
                    };
                }
                inObj = {
                    trx_id : VAL.T_FPPWHSOPE,
                    action_flg : 'A',
                    evt_user : "C1602020",
                    iary : iary
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    showSuccessDialog("Densebox信息添加成功");
                    new_flg = "N";
                    update_flg = "N";
                    btnFunc.button_disabled(VAL.ENABLED_ATTR, 1.00);
                    btnQuery.commit_btn.attr(VAL.DISABLED_ATTR);
                    btnQuery.commit_btn.fadeTo("fast",0.25);
                    $('input[type="text"]').attr(VAL.DISABLED_ATTR);
                    $("select").attr(VAL.DISABLED_ATTR);
                }
            }else if(update_flg == "Y"){
                var inObj, outObj, iary, newRowId;
                var cusId,mtrlProdId,ppBoxStat,mtrlLotId,stbShop,mtrlBoxId,mtrlThickness,prdTlQty;
                mtrlProdId = controlsQuery.mtrlProdId.val().trim();
                ppBoxStat = controlsQuery.ppBoxStat.val().trim();
                mtrlLotId = controlsQuery.mtrlLotId.val().trim();
                stbShop = controlsQuery.stbShop.val().trim();
                storageLoc = controlsQuery.storageLoc.val().trim();
                mtrlBoxId = controlsQuery.mtrlBoxId.val().trim();
                mtrlThickness = controlsQuery.mtrlThickness.val().trim();
                prdTlQty = controlsQuery.prdTlQty.val().trim();
                cusId = controlsQuery.cusId.val().trim();

                if(!mtrlBoxId){
                    showErrorDialog("", "Dense id不能为空");
                    return false;
                }
                if(!cusId){
                    showErrorDialog("", "来料厂商不能为空");
                    return false;
                }
                if(!mtrlProdId){
                    showErrorDialog("", "来料料号不能为空");
                    return false;
                }
                if(!mtrlThickness){
                    showErrorDialog("", "玻璃厚度不能为空");
                    return false;
                }
                if(!prdTlQty){
                    showErrorDialog("", "玻璃数量不能为空");
                    return false;
                }
				/*if(!ppBoxStat){
				 showErrorDialog("", "Dense状态不能为空");
				 return false;
				 }*/
//				  if(!mtrlLotId){
//					  showErrorDialog("", "批次不能为空");
//						return false;
//				  }
                if(!stbShop){
                    showErrorDialog("", "仓位不能为空");
                    return false;
                }
                // if(!storageLoc){
                //   showErrorDialog("", "储位不能为空");
                // 	return false;
                // }
                iary = {
                    cus_id_fk 	    :   cusId 		,
                    mtrl_prod_id_fk :   mtrlProdId 	,
                    ppbox_stat		:   ppBoxStat	,
                    stb_shop		:   stbShop		,
                    storage_loc		:   storageLoc	,
                    mtrl_box_id		:   mtrlBoxId	,
                    mtrl_thickness	:   mtrlThickness,
                    prd_tl_qty		:   prdTlQty	,
                    mtrl_lot_id		:   mtrlLotId

                }
                inObj = {
                    trx_id     : VAL.T_FPPWHSOPE,
                    action_flg : 'U',
                    evt_user   : "C1602020",
                    iary : [iary]
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    showSuccessDialog("Densebox信息更新成功");
                    new_flg = "N";
                    update_flg = "N";
                    btnFunc.button_disabled(VAL.ENABLED_ATTR, 1.00);
                    btnQuery.commit_btn.attr(VAL.DISABLED_ATTR);
                    btnQuery.commit_btn.fadeTo("fast",0.25);
                    $('input[type="text"]').attr(VAL.DISABLED_ATTR);
                    $("select").attr(VAL.DISABLED_ATTR);
                }
            }
        },
        button_disabled: function(disabled_attr,fadeTo){
            btnQuery.add_btn.attr(disabled_attr);
            btnQuery.add_btn.fadeTo("fast",fadeTo);

            btnQuery.query_btn.attr(disabled_attr);
            btnQuery.query_btn.fadeTo("fast",fadeTo);

            btnQuery.update_btn.attr(disabled_attr);
            btnQuery.update_btn.fadeTo("fast",fadeTo);

            btnQuery.delete_btn.attr(disabled_attr);
            btnQuery.delete_btn.fadeTo("fast",fadeTo);

            btnQuery.pick_btn.attr(disabled_attr);
            btnQuery.pick_btn.fadeTo("fast",fadeTo);

            btnQuery.rtGoods_btn.attr(disabled_attr);
            btnQuery.rtGoods_btn.fadeTo("fast",fadeTo);

            btnQuery.rwHouse_btn.attr(disabled_attr);
            btnQuery.rwHouse_btn.fadeTo("fast",fadeTo);

            btnQuery.scrp_btn.attr(disabled_attr);
            btnQuery.scrp_btn.fadeTo("fast",fadeTo);
        },
        new_func: function(){
            new_flg = "Y";
            btnFunc.button_disabled(VAL.DISABLED_ATTR,0.25);
            btnQuery.commit_btn.attr(VAL.ENABLED_ATTR);
            btnQuery.commit_btn.fadeTo("fast",1.0);
            domObj.ppBoxInfoGrd.jqGrid("clearGridData");
            $('input[type="text"]').val("");
            $('input[type="text"]').attr(VAL.ENABLED_ATTR);
            SelectDom.setSelect(controlsQuery.cusId, "");
            SelectDom.setSelect(controlsQuery.mtrlProdId, "");
            SelectDom.setSelect(controlsQuery.ppBoxStat, "");
            SelectDom.setSelect(controlsQuery.stbShop, "");
            SelectDom.setSelect(controlsQuery.storageLoc, "");
            $("select").attr(VAL.ENABLED_ATTR);
            controlsQuery.mtrlBoxId.focus();
        },
        add_func: function(){
            if(new_flg != "Y"){
                showErrorDialog("", "请先点击新增按钮！");
                return false;
            }
            var mtrlProdId = controlsQuery.mtrlProdId.val().trim();
            var ppBoxStat = controlsQuery.ppBoxStat.val().trim();
            var mtrlLotId = controlsQuery.mtrlLotId.val().trim();
            var stbShop = controlsQuery.stbShop.val().trim();
            var storageLoc = controlsQuery.storageLoc.val().trim();
            var mtrlBoxId = controlsQuery.mtrlBoxId.val().trim();
            var mtrlThickness = controlsQuery.mtrlThickness.val().trim();
            var prdTlQty = controlsQuery.prdTlQty.val().trim();
            var cusId = controlsQuery.cusId.val().trim();
            if(!mtrlBoxId){
                showErrorDialog("", "Dense id不能为空");
                return false;
            }
            if(!cusId){
                showErrorDialog("", "来料厂商不能为空");
                return false;
            }
            if(!mtrlProdId){
                showErrorDialog("", "来料料号不能为空");
                return false;
            }
            if(!mtrlThickness){
                showErrorDialog("", "玻璃厚度不能为空");
                return false;
            }
            if(!prdTlQty){
                showErrorDialog("", "玻璃数量不能为空");
                return false;
            }
            if(!stbShop){
                showErrorDialog("", "仓位不能为空");
                return false;
            }
            // if(!storageLoc){
            //   showErrorDialog("", "储位不能为空");
            // 	return false;
            // }
            var row = {
                cus_id_fk 	       :   cusId 			,
                mtrl_prod_id_fk :   mtrlProdId 	,
                ppbox_stat		   :   ppBoxStat		,
                stb_shop		   :   stbShop		,
                storage_loc	   :   storageLoc		,
                mtrl_box_id	   :   mtrlBoxId		,
                mtrl_thickness   :   mtrlThickness	,
                prd_tl_qty		   :   prdTlQty		,
                mtrl_lot_id	   :   mtrlLotId
            };
            var firstRow = $("#ppBoxInfoGrd").jqGrid("getRowData");
            if (firstRow.length>0){
                for (var i = 0; i < firstRow.length; i++) {
                    if (firstRow[i].mtrl_box_id === mtrlBoxId) {
                        controlsQuery.mtrlBoxId.val("");
                        return;
                    }
                }
                if (firstRow[0].cus_id_fk != cusId || firstRow[0].mtrl_prod_id_fk != mtrlProdId ||
                    firstRow[0].mtrl_thickness != mtrlThickness ||firstRow[0].stb_shop != stbShop ||
                    firstRow[0].storage_loc != storageLoc)
                {
                    showErrorDialog("", "加入表中的客户，来料型号，玻璃厚度，仓别，储位必须一致！");
                    return false;
                }
            }
            var newRowID = getGridNewRowID("#ppBoxInfoGrd");
            domObj.ppBoxInfoGrd.jqGrid('addRowData',newRowID,row);
            controlsQuery.mtrlBoxId.val("");
        },
        pick_func: function(){
            var iary = new Array();
            var rowIds = domObj.ppBoxInfoGrd.jqGrid('getGridParam', 'selarrrow');
            if(rowIds.length === 0 ){
                showErrorDialog("","请选择需要领料的箱子！");
                return false;
            }
            btnQuery.pick_btn.showCallBackWarnningDialog({
                errMsg : "是否进行领料操作,请确认!!!!",
                callbackFn : function(data) {
                    var iaryA;
                    if (data.result == true) {
                        for (var int = 0; int < rowIds.length; int++) {
                            var rows = domObj.ppBoxInfoGrd.jqGrid('getRowData',rowIds[int]);
                            iaryA = {
                                mtrl_box_id:rows["mtrl_box_id"],
                            };
                            iary[int] = iaryA;
                        }
                        inObj = {
                            trx_id : VAL.T_FPPWHSOPE,
                            action_flg : "P",
                            box_cnt : rowIds.length,
                            iary : iary
                        };
                        outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            showSuccessDialog("领料操作成功");
                            toolFunc.updateGrdInput(rowIds,'OUT');
                        }
                    }

                }
            });
        },
        rwHouse_func : function(){
            var iary = new Array();
            var rowIds = domObj.ppBoxInfoGrd.jqGrid('getGridParam', 'selarrrow');
            if(rowIds.length === 0 ){
                showErrorDialog("","请选择需要退仓的箱子！");
                return false;
            }
            btnQuery.rwHouse_btn.showCallBackWarnningDialog({
                errMsg : "是否进行退仓操作,请确认!!!!",
                callbackFn : function(data) {
                    var iaryA;
                    if (data.result == true) {
                        for (var int = 0; int < rowIds.length; int++) {
                            var rows = domObj.ppBoxInfoGrd.jqGrid('getRowData',rowIds[int]);
                            iaryA = {
                                mtrl_box_id:rows["mtrl_box_id"],
                            };
                            iary[int] = iaryA;
                        }
                        inObj = {
                            trx_id : VAL.T_FPPWHSOPE,
                            action_flg : "H",
                            box_cnt : rowIds.length,
                            iary : iary
                        };
                        outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            showSuccessDialog("退仓操作成功");
                            toolFunc.updateGrdInput(rowIds,'IN');
                        }
                    }

                }
            });
        },
        rtGoods_func : function(){
            var iary = new Array();
            var rowIds = domObj.ppBoxInfoGrd.jqGrid('getGridParam', 'selarrrow');
            if(rowIds.length === 0 ){
                showErrorDialog("","请选择需要退货的箱子！");
                return false;
            }
            btnQuery.rtGoods_btn.showCallBackWarnningDialog({
                errMsg : "是否进行退货操作,请确认!!!!",
                callbackFn : function(data) {
                    var iaryA;
                    if (data.result == true) {
                        for (var int = 0; int < rowIds.length; int++) {
                            var rows = domObj.ppBoxInfoGrd.jqGrid('getRowData',rowIds[int]);
                            iaryA = {
                                mtrl_box_id:rows["mtrl_box_id"],
                            };
                            iary[int] = iaryA;
                        }
                        inObj = {
                            trx_id : VAL.T_FPPWHSOPE,
                            action_flg : "G",
                            box_cnt : rowIds.length,
                            iary : iary
                        };
                        outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            showSuccessDialog("退货操作成功");
                            toolFunc.updateGrdInput(rowIds,'RTCS');
                        }
                    }

                }
            });
        },
        scrp_func : function(){
            var iary = new Array();
            var rowIds = domObj.ppBoxInfoGrd.jqGrid('getGridParam', 'selarrrow');
            if(rowIds.length === 0 ){
                showErrorDialog("","请选择需要报废的箱子！");
                return false;
            }
            btnQuery.scrp_btn.showCallBackWarnningDialog({
                errMsg : "是否进行报废操作,请确认!!!!",
                callbackFn : function(data) {
                    var iaryA;
                    if (data.result == true) {
                        for (var int = 0; int < rowIds.length; int++) {
                            var rows = domObj.ppBoxInfoGrd.jqGrid('getRowData',rowIds[int]);
                            iaryA = {
                                mtrl_box_id:rows["mtrl_box_id"],
                            };
                            iary[int] = iaryA;
                        }
                        inObj = {
                            trx_id : VAL.T_FPPWHSOPE,
                            action_flg : "S",
                            box_cnt : rowIds.length,
                            iary : iary
                        };
                        outObj = comTrxSubSendPostJson(inObj);
                        if (outObj.rtn_code === VAL.NORMAL) {
                            showSuccessDialog("报废操作成功");
                            toolFunc.updateGrdInput(rowIds,'SCRP');
                        }
                    }

                }

            });
        },
        jqGridDel_func : function(){
            if(new_flg != "Y"){
                showErrorDialog("","该按钮在新增时才可以使用！");
                return;
            }
            var ids = domObj.ppBoxInfoGrd.jqGrid('getGridParam','selarrrow');
            if (ids.length === 0) {
                showErrorDialog("","请在表中选择需要删除的数据！");
                return;
            }
            for (var i = 0; i < ids.length; i++) {
                domObj.ppBoxInfoGrd.jqGrid("delRowData",ids[i]);
            }
        }
    }

    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        btnQuery.query_btn.click(function(){
            btnFunc.query_func();
        });
        btnQuery.delete_btn.click(function(){
            btnFunc.delete_func();
        });
        btnQuery.commit_btn.click(function(){
            btnFunc.commit_func();
        });
        btnQuery.add_btn.click(function(){
            btnFunc.new_func();
        });
        btnQuery.update_btn.click(function(){
            btnFunc.update_func();
        });
        btnQuery.pick_btn.click(function(){
            btnFunc.pick_func();
        });
        btnQuery.rwHouse_btn.click(function(){
            btnFunc.rwHouse_func();
        });
        btnQuery.rtGoods_btn.click(function(){
            btnFunc.rtGoods_func();
        });
        btnQuery.scrp_btn.click(function(){
            btnFunc.scrp_func();
        });
        btnQuery.jqGridDel_btn.click(function(){
            btnFunc.jqGridDel_func();
        });
        btnQuery.clear_btn.click(function(){
            iniContorlData();
        });
        controlsQuery.stbShop.change(function(){
            toolFunc.iniStorageLocSel();
        });
        controlsQuery.cusId.change(function(){
            toolFunc.iniMtrlProdIdSel();
        });
        controlsQuery.mtrlBoxId.keydown(function(event){
            if (event.keyCode == 13) {
                btnFunc.add_func();
            }
        });
    };

    var iniContorlData = function(){
        toolFunc.initFnc();
        toolFunc.iniCusIdSel(controlsQuery.cusId);
//		toolFunc.iniMtrlProdIdSel(controlsQuery.mtrlProdId);
        toolFunc.iniStbShopSel();
        toolFunc.iniBoxStatSel();
        new_flg = "N";
        update_flg ="N";
        btnFunc.button_disabled(VAL.ENABLED_ATTR,1.0);
        btnQuery.commit_btn.attr(VAL.DISABLED_ATTR);
        btnQuery.commit_btn.fadeTo("fast",0.25);
    };
    var initializationFunc = function(){
        iniGridInfo();
        iniButtonAction();
        iniContorlData();
    };
    initializationFunc();
//模态框拖动
    $(".modal-content").draggable();
    //表格自适应
    function resizeFnc(){
        var offsetBottom, grdDivWidth;
        grdDivWidth = $("#ppBoxInfoDiv").width();
        offsetBottom = $(window).height() - $("#ppBoxInfoDiv").offset().top-20;

        $("#ppBoxInfoDiv").width(grdDivWidth*0.98);
        $("#ppBoxInfoDiv").height(offsetBottom*0.99);
        $("#ppBoxInfoGrd").setGridWidth(grdDivWidth*0.97);
        $("#ppBoxInfoGrd").setGridHeight(offsetBottom*0.95-170);
    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });



});